﻿@model DiscountModel.AddManufacturerToDiscountModel

@{
    Layout = "~/Administration/Views/Shared/_AdminPopupLayout.cshtml";

    var popupGridPageSize = EngineContext.Current.Resolve<Nop.Core.Domain.Common.AdminAreaSettings>().PopupGridPageSize;

    //page title
    ViewBag.Title = T("Admin.Promotions.Discounts.AppliedToManufacturers.AddNew").Text;
}

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()
    <div class="content-header clearfix">
        <h1 class="pull-left">
            @T("Admin.Promotions.Discounts.AppliedToManufacturers.AddNew")
        </h1>
        <div class="pull-right">
            &nbsp;
        </div>
    </div>

    if (ViewBag.RefreshPage == true)
    {
        <script type="text/javascript">
            try {window.opener.document.forms['@(ViewBag.formId)'].@(ViewBag.btnId).click();}
            catch (e){}
            window.close();
        </script>
    }

    <div class="content">
        <div class="form-horizontal">
            <div class="panel-group">
                <div class="panel panel-default panel-search panel-popup">
                    <div class="panel-body">
                        <div class="row">
                            <div class="col-sm-8">
                                <div class="form-group">
                                    <div class="col-sm-4">
                                        @Html.NopLabelFor(model => model.SearchManufacturerName)
                                    </div>
                                    <div class="col-sm-8">
                                        @Html.NopEditorFor(model => model.SearchManufacturerName)
                                    </div>
                                </div>
                                <div class="form-group">
                                    <div class="col-sm-8 col-sm-offset-4">
                                        <button type="button" id="search-manufacturers" class="btn btn-primary btn-search">
                                            <i class="fa fa-search"></i>
                                            @T("Admin.Common.Search")
                                        </button>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

                <div class="panel panel-default">
                    <div class="panel-body">
                        <div id="manufacturers-grid"></div>

                        <script>
                            $(document).ready(function () {
                                $("#manufacturers-grid").kendoGrid({
                                    dataSource: {
                                        type: "json",
                                        transport: {
                                            read: {
                                                url: "@Html.Raw(Url.Action("ManufacturerAddPopupList", "Discount"))",
                                                type: "POST",
                                                dataType: "json",
                                                data: additionalData
                                            }
                                        },
                                        schema: {
                                            data: "Data",
                                            total: "Total",
                                            errors: "Errors"
                                        },
                                        error: function(e) {
                                            display_kendoui_grid_error(e);
                                            // Cancel the changes
                                            this.cancelChanges();
                                        },
                                        pageSize: @(popupGridPageSize),
                                        serverPaging: true,
                                        serverFiltering: true,
                                        serverSorting: true
                                    },
                                    pageable: {
                                        refresh: true,
                                        @Html.Partial("_GridPagerMessages")
                                    },
                                    editable: {
                                        confirmation: "@T("Admin.Common.DeleteConfirmation")",
                                        mode: "inline"
                                    },
                                    scrollable: false,
                                    columns: [{
                                        field: "Id",
                                        headerTemplate: "<input id='mastercheckbox' type='checkbox'/>",
                                        headerAttributes: { style: "text-align:center" },
                                        template: "<input type='checkbox' name='SelectedManufacturerIds' value='#=Id#' class='checkboxGroups' />",
                                        attributes: { style: "text-align:center" },
                                        width: 50
                                    }, {
                                        field: "Name",
                                        title: "@T("Admin.Catalog.Manufacturers.Fields.Name")"
                                    }, {
                                        field: "Published",
                                        title: "@T("Admin.Catalog.Manufacturers.Fields.Published")",
                                        width: 100,
                                        headerAttributes: { style: "text-align:center" },
                                        attributes: { style: "text-align:center" },
                                        template: '# if(Published) {# <i class="fa fa-check true-icon"></i> #} else {# <i class="fa fa-close false-icon"></i> #} #'
                                    }]
                                });
                            });
                        </script>

                        <script type="text/javascript">
                            $(document).ready(function() {
                                $('#search-manufacturers').click(function() {
                                    var grid = $('#manufacturers-grid').data('kendoGrid');
                                    grid.dataSource.page(1); //new search. Set page size to 1
                                    //grid.dataSource.read(); we already loaded the grid above using "page" function
                                    //clear selected checkboxes
                                    $('.checkboxGroups').attr('checked', false).change();
                                    return false;
                                });

                                $("#@Html.FieldIdFor(model => model.SearchManufacturerName)").keydown(function(event) {
                                    if (event.keyCode == 13) {
                                        $("#search-manufacturers").click();
                                        return false;
                                    }
                                });

                                //wire up checkboxes
                                $('#mastercheckbox').click(function() {
                                    $('.checkboxGroups').attr('checked', $(this).is(':checked')).change();
                                });

                                $('#manufacturers-grid').on('change', 'input[type=checkbox][id!=mastercheckbox][class=checkboxGroups]', function(e) {
                                    updateMasterCheckbox();
                                });
                            });

                            function additionalData() {
                                var data = {
                                    SearchManufacturerName: $('#@Html.FieldIdFor(model => model.SearchManufacturerName)').val()
                                };
                                addAntiForgeryToken(data);
                                return data;
                            }

                            function updateMasterCheckbox() {
                                var numChkBoxes = $('#manufacturers-grid input[type=checkbox][id!=mastercheckbox][class=checkboxGroups]').length;
                                var numChkBoxesChecked = $('#manufacturers-grid input[type=checkbox][id!=mastercheckbox][class=checkboxGroups]:checked').length;
                                $('#mastercheckbox').attr('checked', numChkBoxes == numChkBoxesChecked && numChkBoxes > 0);
                            }

                        </script>
                    </div>
                    <div class="panel-footer">
                        <button type="submit" name="save" class="btn bg-blue">
                            <i class="fa fa-floppy-o"></i>
                            @T("Admin.Common.Save")
                        </button>
                    </div>
                </div>
            </div>
        </div>
    </div>
}
